#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_task_breakpoint.py 
@version:
@time: 2022/03/25 
"""
import time

from TapClientCore.T07_CaseTemplate.v2.CT03_task.ct_task import CTTask, allure, logger


class CTTaskBreakPoint(CTTask):

    def task_template_break_point(self, parallel_data):
        data = None
        # 随机等待
        self.random_wait()
        # 并行案例初始化
        data = self.lock_and_only_get_table(parallel_data)
        record_id = data.get('id')

        operate = data.pop('operate')
        operate_source = operate.get('source')
        source_info = operate_source.get('source_info')
        source_table = operate_source.get('table')
        operate_target = operate.get('target')
        target_info = operate_target.get('target_info')
        target_table = operate_target.get('table')

        data['task_config']['config']['name'] += source_table
        with allure.step('创建表获取'):
            task_name = data.get('task_config').get('config').get('name')

        # connector
        with allure.step('源数据库初始化'):
            _source = self.init_connector(source_info)
        with allure.step('目标数据库初始化'):
            _target = self.init_connector(target_info)

        _target.tapclient_connector.CURRENT_TABLE = target_table

        # stats check
        _stats = operate.get('stats_check').get('initial')
        _stats_initial = _stats.get('insert')
        self.drop_target_table(_target)
        try:
            with allure.step('创建任务'):
                self.create_data_copy_development(data)
            # initial stats insert 校验
            with allure.step('任务状态等待'):
                self.scheduled_task_wait_it_running(task_name)
            time.sleep(10)
            with allure.step('停止任务'):
                self.stop_task_wait_it_paused(task_name)
            with allure.step('再次启动任务'):
                self.scheduled_task_wait_it_complete(task_name)
            self.func_list_wait(
                self.get_task_stats_insert,
                self.get_task_status,
                task_name,
                _stats_initial,
            )
        finally:
            self.count_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            self.generate_task_url(task_name, data.get('task_type'))
            logger.info(self.task_url)
            try:
                _source.tapclient_connector.close()
                _target.tapclient_connector.close()
            except Exception as e:
                pass

    def task_template_not_break_point_initial_count_check(self, parallel_data):
        """
                全量不校验任务运行,并行执行任务
                :param parallel_data:
                :return:
                """
        data = None
        # 随机等待
        self.random_wait()
        # 并行案例初始化
        data = self.lock_and_only_get_table(parallel_data)
        record_id = data.get('id')

        operate = data.pop('operate')
        operate_source = operate.get('source')
        source_info = operate_source.get('source_info')
        source_table = operate_source.get('table')
        operate_target = operate.get('target')
        target_info = operate_target.get('target_info')
        target_table = operate_target.get('table')

        data['task_config']['config']['name'] += source_table
        with allure.step('创建表获取'):
            task_name = data.get('task_config').get('config').get('name')

        # connector
        with allure.step('源数据库初始化'):
            _source = self.init_connector(source_info)
        with allure.step('目标数据库初始化'):
            _target = self.init_connector(target_info)

        _target.tapclient_connector.CURRENT_TABLE = target_table

        source_where_column = operate_source.get('where')
        source_delete = _source.tapclient_connector.generate_delete(
            source_where_column
        )
        # stats check
        _stats = operate.get('stats_check').get('initial')
        _stats_initial = _stats.get('insert')
        self.drop_target_table(_target)
        try:
            with allure.step('创建任务'):
                self.create_data_copy_development(data)
            # initial stats insert 校验
            with allure.step('任务状态等待'):
                self.scheduled_task_wait_it_complete(task_name)
            self.func_wait(
                self.get_task_stats_insert,
                self.get_task_status,
                task_name,
                _stats_initial
            )
        finally:
            # check
            self.count_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            self.generate_task_url(task_name, data.get('task_type'))
            logger.info(self.task_url)
            try:
                _source.tapclient_connector.close()
                _target.tapclient_connector.close()
            except Exception as e:
                pass
